package game.combat;

import character.CharacterCombatControl;
import character.MyCharacter;
import com.jme3.app.state.AbstractAppState;
import game.DataStore;
import game.InputController;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ResolutionAppState extends AbstractAppState {
    protected CombatAppState combatAppState;
    protected ResolutionInputHandler resolutionInput = new ResolutionInputHandler();
    protected float timeSinceLastTick = 0f;
    protected ResolutionListener characterListener;
    protected List<ResolutionListener> temporaryListeners = new ArrayList<ResolutionListener>();
    protected List<ResolutionListener> constantListeners = new ArrayList<ResolutionListener>();
    protected int tickNum = 1; //just here for debugging

    @Deprecated
    protected ResolutionAppState() {}

    public ResolutionAppState(CombatAppState combatAppState) {
	this.combatAppState = combatAppState;
    }

    @Override
    public void setEnabled(boolean enabled) {
	super.setEnabled(enabled);
	if(enabled) {
	    InputController.setCurrentGameInput(resolutionInput);
	    timeSinceLastTick = 0f;
	    startResolution();
	}
    }
    
    @Override
    public void update(float tpf) {
	super.update(tpf);
	timeSinceLastTick += tpf;
	if(timeSinceLastTick >= DataStore.TIME_PER_TICK) {
	    tick();
	    timeSinceLastTick = 0f;
	}
    }

    public void startResolution() {
        tickNum = 1;
	characterListener.resolutionStarted();
	for(ResolutionListener listener : temporaryListeners)
	    listener.resolutionStarted();
        for(ResolutionListener listener : constantListeners)
            listener.resolutionStarted();
    }

    public void tick() {
        if(((CharacterCombatControl)characterListener).hasQueuedAction() && characterListener.getTicksUsed() < ((MyCharacter)combatAppState.getCurrentCombatActor()).getCharacterStatus().getTicksPerTurn()) {
            System.out.println("tickNum: "+tickNum);
            if(characterListener.tick()) {
                tickTemporaryListeners();
                tickConstantListeners();
                tickNum++;
            }
        } else {
            endResolution();
        }
    }

    protected void tickTemporaryListeners() {
	Iterator<ResolutionListener> iter = temporaryListeners.iterator();
	while(iter.hasNext()) {
	    ResolutionListener tempListener = iter.next();
	    tempListener.tick();
	    if(tempListener.isMarkedForTemporaryResolutionListenerRemoval()) {
		tempListener.setMarkedForTemporaryResolutionListenerRemoval(false);
		iter.remove();
	    }
	}
    }

    protected void tickConstantListeners() {
	for(ResolutionListener listener : constantListeners) {
	    listener.tick();
	}
    }

    protected void endResolution() {
	characterListener.resolutionEnded();
	for(ResolutionListener listener : temporaryListeners)
	    listener.resolutionEnded();
        for(ResolutionListener listener : constantListeners)
            listener.resolutionEnded();
        temporaryListeners.clear();
	characterListener = null;
	combatAppState.resolutionEnded();
    }

    public void registerConstantListener(ResolutionListener listener) {
        constantListeners.add(listener);
    }

    /** Queues a listener to listen to ticks for the rest of this round. */
    public void registerTemporaryListener(ResolutionListener listener) {
	temporaryListeners.add(listener);
    }
    public void removeTemporaryListener(ResolutionListener listener) {
        temporaryListeners.remove(listener);
    }

    public void setCharacterListener(ResolutionListener characterListener) {
	this.characterListener = characterListener;
    }
}
